package xyz.weizhi.tanhua.appserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.weizhi.tanhua.appserver.service.MovementsService;
import xyz.weizhi.tanhua.model.enums.CommentType;
import xyz.weizhi.tanhua.model.mongo.Movement;
import xyz.weizhi.tanhua.model.vo.MovementsVo;
import xyz.weizhi.tanhua.model.vo.PageResult;

import java.io.IOException;
import java.util.Map;

/**
 * @Author milian
 * @Date 2021/10/25 0025 20:33
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;
    /*
        动态-发布
     */
    @PostMapping
    public ResponseEntity pubulish(Movement movement, MultipartFile[] files) throws IOException {
        movementsService.pubulish(movement,files);
        return null;
    }
    /*
        我的动态
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementsService.findByUserId(userId, page, pagesize);


        return ResponseEntity.ok(pageResult);
    }
    /*
        好友动态
     */
    @GetMapping
    public ResponseEntity friendMovement( @RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementsService.findByfriendId(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
    /*
        推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementsService.recommend(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
    /*
       单条动态
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId){
        MovementsVo vo = movementsService.findById(movementId);
        return ResponseEntity.ok(vo);
    }
   /*
        点赞、取消点赞、喜欢、取消喜欢
     */
    @GetMapping("/{id}/{operate}")
    public ResponseEntity operate(@PathVariable("id")String id, @PathVariable("operate") String operate){
        int type;
        // 正向操作
        boolean flag=true;
        if (operate.equals("like")){
            type=CommentType.LIKE.getType();
        }else if(operate.equals("dislike")){
            flag = false;
            type=CommentType.LIKE.getType();
        }else if (operate.equals("love")){
            type=CommentType.LOVE.getType();
        }else {
            type=CommentType.LOVE.getType();
            flag = false;
        }
        Integer count = movementsService.operate(id, flag, type);
        return ResponseEntity.ok(count);
    }
}
